package com.yanqu.road.entity.activity.jieshi.config;

import com.yanqu.road.entity.activity.ActivityConfig;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.enums.eActivityConfigType;
import com.yanqu.road.utils.RandomHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

public class JieShiConfig {

    private RandomHelper randomHelper = new RandomHelper();

    private ActivityInfo activityInfo;

    /**
     * 街市等级表
     */
    private Map<Integer, LightMarketMainLvConfig> mainLvConfigMap = new ConcurrentHashMap<>();

    /**
     * 商铺表
     */
    private Map<Integer, LightMarketShopConfig> shopConfigMap = new ConcurrentHashMap<>();

    /**
     * 街道司表
     */
    private Map<Integer, LightMarketPatronConfig> patronConfigMap = new ConcurrentHashMap<>();

    /**
     * 节奏表
     */
    private Map<Integer, LightMarketRhythmConfig> rhythmConfigMap = new ConcurrentHashMap<>();

    /**
     * 事件表
     */
    private Map<Integer, LightMarketEventConfig> eventConfigMap = new ConcurrentHashMap<>();

    /**
     * 地图数据
     */
    private Map<Integer, Integer> jieShiMap = new HashMap<>();

    /**
     * 地图npc数据
     */
    private List<Integer> jieShiNpcPos = new ArrayList<>();

    /**
     * 获取地图路径
     */
    private List<Integer> jieShiMapPath = new ArrayList<>();

    /**
     * 获取地图宽度（计算坐标用）
     */
    private int mapWidth;


    // 活动配置


    /**
     * 体力道具id（鱼龙灯）
     */
    private int LIGHTMARKET_ENERGY_ITEM_ID;

    /**
     * 交子道具id
     */
    private int LIGHTMARKET_MONEY_ITEM_ID;

    /**
     * 武力人偶道具id
     */
    private int LIGHTMARKET_DOY_WULI_ITEM_ID;

    /**
     * 交子人偶道具id
     */
    private int LIGHTMARKET_DOY_JIAOZI_ITEM_ID;

    /**
     * 默认人偶id
     */
    private int LIGHTMARKET_DOY_DEFAULT_ITEM_ID;

    /**
     * 体力上限
     */
    private int LIGHTMARKET_ENERGY_MAX_NUM;

    /**
     * 体力每次恢复数量
     */
    private int LIGHTMARKET_ENERGY_RECOVERY_NUM;

    /**
     * 体力恢复时间间隔（秒）
     */
    private int LIGHTMARKET_ENERGY_RECOVERY_TIME;

    /**
     * 圈数对应每圈体力消耗（圈数最小值；每圈消耗体力|下一阶段圈数最小值；每圈消耗体力……）
     */
    private String LIGHTMARKET_ROUND_COST;

    /**
     * 门客拜访事件，精进等级对应货铺加成效果千分比
     */
    private String LIGHTMARKET_PARTON_VISIT;

    /**
     * 解锁地块消耗交子（按次数）
     */
    private String LIGHTMARKET_MAP_UNLOCK_COST;

    /**
     *  门客赚钱转武力，单个门客赚钱超过一个值则按最高武力计算（超过值|转换武力）
     */
    private String LIGHTMARKET_PARTON_TURN_LIMIT_MAX;

    /**
     * 门客赚钱转武力，单个门客赚钱小于一个值则按最低武力计算（小于值|转换武力）
     */
    private String LIGHTMARKET_PARTON_TURN_LIMIT_MIN;

    /**
     * 门客赚钱赚武力公式参数（二次函数a、b、c）千分比
     */
    private String LIGHTMARKET_PARTON_TURN_PARAM;

    /**
     * 本次地图id
     */
    private int LIGHTMARKET_MAP_ID_CHOOSE;

    /**
     * 每点体力获得积分数量
     */
    private int LIGHTMARKET_SCORE_GET_NUM;

    /**
     * 积分道具id
     */
    private int EXCHANGE_SCORE_ITEM_ID;


    /**
     * 初始时辰
     */
    private int LIGHTMARKET_MAP_BEGIN_TIME;

    /**
     * 街市随机库名字数量
     */
    private int LIGHTMARKET_NAME_NUM;

    /**
     * 泼皮道具奖励（圈数最小值|奖励#下一区间圈数最小值|奖励……)
     */
    private String LIGHTMARKET_RASCAL_REWARD;

    /**
     * 泼皮生成点配置（圈数最小值|生成点列表#下一区间最小值|生成点列表……）
     */
    private String LIGHTMARKET_RASCAL_POINT;

    /**
     * 泼皮名字数量
     */
    private int LIGHTMARKET_RASCAL_NAME_NUM;

    /**
     * 重置地块消耗
     */
    private String LIGHTMARKET_RESET_COST;

    /**
     * 初始化相关配置
     *
     * @param activityConfigMap 活动配置map
     */
    public void initConfig(Map<String, ActivityConfig> activityConfigMap) {
        LIGHTMARKET_ENERGY_ITEM_ID = activityConfigMap.get(eActivityConfigType.LIGHTMARKET_ENERGY_ITEM_ID.getName()).getIntValue();
        LIGHTMARKET_MONEY_ITEM_ID = activityConfigMap.get(eActivityConfigType.LIGHTMARKET_MONEY_ITEM_ID.getName()).getIntValue();
        String[] doyItemSplit = activityConfigMap.get(eActivityConfigType.LIGHTMARKET_DOY_ITEM_ID.getName()).getValue().split("\\|");
        LIGHTMARKET_DOY_WULI_ITEM_ID = Integer.parseInt(doyItemSplit[0]);
        LIGHTMARKET_DOY_JIAOZI_ITEM_ID = Integer.parseInt(doyItemSplit[1]);
        LIGHTMARKET_ENERGY_MAX_NUM = activityConfigMap.get(eActivityConfigType.LIGHTMARKET_ENERGY_MAX_NUM.getName()).getIntValue();
        LIGHTMARKET_ENERGY_RECOVERY_NUM = activityConfigMap.get(eActivityConfigType.LIGHTMARKET_ENERGY_RECOVERY_NUM.getName()).getIntValue();
        LIGHTMARKET_ENERGY_RECOVERY_TIME = activityConfigMap.get(eActivityConfigType.LIGHTMARKET_ENERGY_RECOVERY_TIME.getName()).getIntValue();
        LIGHTMARKET_ROUND_COST = activityConfigMap.get(eActivityConfigType.LIGHTMARKET_ROUND_COST.getName()).getValue();
        LIGHTMARKET_PARTON_VISIT = activityConfigMap.get(eActivityConfigType.LIGHTMARKET_PARTON_VISIT.getName()).getValue();
        LIGHTMARKET_MAP_UNLOCK_COST = activityConfigMap.get(eActivityConfigType.LIGHTMARKET_MAP_UNLOCK_COST.getName()).getValue();
        LIGHTMARKET_DOY_DEFAULT_ITEM_ID = activityConfigMap.get(eActivityConfigType.LIGHTMARKET_DOY_DEFAULT_ITEM_ID.getName()).getIntValue();
        LIGHTMARKET_PARTON_TURN_LIMIT_MAX = activityConfigMap.get(eActivityConfigType.LIGHTMARKET_PARTON_TURN_LIMIT_MAX.getName()).getValue();
        LIGHTMARKET_PARTON_TURN_LIMIT_MIN = activityConfigMap.get(eActivityConfigType.LIGHTMARKET_PARTON_TURN_LIMIT_MIN.getName()).getValue();
        LIGHTMARKET_PARTON_TURN_PARAM = activityConfigMap.get(eActivityConfigType.LIGHTMARKET_PARTON_TURN_PARAM.getName()).getValue();
        LIGHTMARKET_MAP_ID_CHOOSE = activityConfigMap.get(eActivityConfigType.LIGHTMARKET_MAP_ID_CHOOSE.getName()).getIntValue();
        LIGHTMARKET_SCORE_GET_NUM = activityConfigMap.get(eActivityConfigType.LIGHTMARKET_SCORE_GET_NUM.getName()).getIntValue();
        EXCHANGE_SCORE_ITEM_ID = activityConfigMap.get(eActivityConfigType.EXCHANGE_SCORE_ITEM_ID.getName()).getIntValue();
        LIGHTMARKET_MAP_BEGIN_TIME = activityConfigMap.get(eActivityConfigType.LIGHTMARKET_MAP_BEGIN_TIME.getName()).getIntValue();
        LIGHTMARKET_NAME_NUM = activityConfigMap.get(eActivityConfigType.LIGHTMARKET_NAME_NUM.getName()).getIntValue();
        LIGHTMARKET_RASCAL_REWARD = activityConfigMap.get(eActivityConfigType.LIGHTMARKET_RASCAL_REWARD.getName()).getValue();
        LIGHTMARKET_RASCAL_POINT = activityConfigMap.get(eActivityConfigType.LIGHTMARKET_RASCAL_POINT.getName()).getValue();
        LIGHTMARKET_RASCAL_NAME_NUM = activityConfigMap.get(eActivityConfigType.LIGHTMARKET_RASCAL_NAME_NUM.getName()).getIntValue();
        LIGHTMARKET_RESET_COST = activityConfigMap.get(eActivityConfigType.LIGHTMARKET_RESET_COST.getName()).getValue();
    }

    public JieShiConfig() {

    }

    public ActivityInfo getActivityInfo() {
        return activityInfo;
    }

    public void setActivityInfo(ActivityInfo activityInfo) {
        this.activityInfo = activityInfo;
    }

    public Map<Integer, LightMarketMainLvConfig> getMainLvConfigMap() {
        return mainLvConfigMap;
    }

    public void setMainLvConfigMap(Map<Integer, LightMarketMainLvConfig> mainLvConfigMap) {
        this.mainLvConfigMap = mainLvConfigMap;
    }

    public Map<Integer, LightMarketShopConfig> getShopConfigMap() {
        return shopConfigMap;
    }

    public void setShopConfigMap(Map<Integer, LightMarketShopConfig> shopConfigMap) {
        this.shopConfigMap = shopConfigMap;
    }

    public Map<Integer, LightMarketPatronConfig> getPatronConfigMap() {
        return patronConfigMap;
    }

    public void setPatronConfigMap(Map<Integer, LightMarketPatronConfig> patronConfigMap) {
        this.patronConfigMap = patronConfigMap;
    }

    public Map<Integer, LightMarketRhythmConfig> getRhythmConfigMap() {
        return rhythmConfigMap;
    }

    public void setRhythmConfigMap(Map<Integer, LightMarketRhythmConfig> rhythmConfigMap) {
        this.rhythmConfigMap = rhythmConfigMap;
    }

    public Map<Integer, LightMarketEventConfig> getEventConfigMap() {
        return eventConfigMap;
    }

    public void setEventConfigMap(Map<Integer, LightMarketEventConfig> eventConfigMap) {
        this.eventConfigMap = eventConfigMap;
    }

    public int getLIGHTMARKET_ENERGY_ITEM_ID() {
        return LIGHTMARKET_ENERGY_ITEM_ID;
    }

    public int getLIGHTMARKET_MONEY_ITEM_ID() {
        return LIGHTMARKET_MONEY_ITEM_ID;
    }

    public int getLIGHTMARKET_DOY_WULI_ITEM_ID() {
        return LIGHTMARKET_DOY_WULI_ITEM_ID;
    }

    public int getLIGHTMARKET_DOY_JIAOZI_ITEM_ID() {
        return LIGHTMARKET_DOY_JIAOZI_ITEM_ID;
    }

    public int getLIGHTMARKET_ENERGY_MAX_NUM() {
        return LIGHTMARKET_ENERGY_MAX_NUM;
    }

    public int getLIGHTMARKET_ENERGY_RECOVERY_NUM() {
        return LIGHTMARKET_ENERGY_RECOVERY_NUM;
    }

    public int getLIGHTMARKET_ENERGY_RECOVERY_TIME() {
        return LIGHTMARKET_ENERGY_RECOVERY_TIME;
    }

    public String getLIGHTMARKET_ROUND_COST() {
        return LIGHTMARKET_ROUND_COST;
    }

    public String getLIGHTMARKET_PARTON_VISIT() {
        return LIGHTMARKET_PARTON_VISIT;
    }

    public String getLIGHTMARKET_MAP_UNLOCK_COST() {
        return LIGHTMARKET_MAP_UNLOCK_COST;
    }

    public int getLIGHTMARKET_DOY_DEFAULT_ITEM_ID() {
        return LIGHTMARKET_DOY_DEFAULT_ITEM_ID;
    }

    public String getLIGHTMARKET_PARTON_TURN_LIMIT_MAX() {
        return LIGHTMARKET_PARTON_TURN_LIMIT_MAX;
    }

    public String getLIGHTMARKET_PARTON_TURN_LIMIT_MIN() {
        return LIGHTMARKET_PARTON_TURN_LIMIT_MIN;
    }

    public String getLIGHTMARKET_PARTON_TURN_PARAM() {
        return LIGHTMARKET_PARTON_TURN_PARAM;
    }

    public int getLIGHTMARKET_MAP_ID_CHOOSE() {
        return LIGHTMARKET_MAP_ID_CHOOSE;
    }

    public int getLIGHTMARKET_MAP_BEGIN_TIME() {
        return LIGHTMARKET_MAP_BEGIN_TIME;
    }

    public int getLIGHTMARKET_SCORE_GET_NUM() {
        return LIGHTMARKET_SCORE_GET_NUM;
    }

    public int getEXCHANGE_SCORE_ITEM_ID() {
        return EXCHANGE_SCORE_ITEM_ID;
    }

    public int getLIGHTMARKET_NAME_NUM() {
        return LIGHTMARKET_NAME_NUM;
    }

    public String getLIGHTMARKET_RASCAL_POINT() {
        return LIGHTMARKET_RASCAL_POINT;
    }

    public int getLIGHTMARKET_RASCAL_NAME_NUM() {
        return LIGHTMARKET_RASCAL_NAME_NUM;
    }

    public String getLIGHTMARKET_RASCAL_REWARD() {
        return LIGHTMARKET_RASCAL_REWARD;
    }

    public String getLIGHTMARKET_RESET_COST() {
        return LIGHTMARKET_RESET_COST;
    }

    /**
     * 根据圈数获取泼皮奖励
     *
     * @param round 圈数
     */
    public String getRascalReward(int round) {
        String reward = "";
        List<String> rewardList = StringUtils.stringToStringList(LIGHTMARKET_RASCAL_REWARD, "#");
        for (String rewardParam : rewardList) {
            String[] split = rewardParam.split("\\|");
            if (round >= Integer.parseInt(split[0])) {
                reward = split[1];
            }
        }
        return reward;
    }

    /**
     * 根据圈数获取泼皮生成的地点 （索引）
     *
     * @param round 圈数
     */
    public List<Integer> getRascalPoint(int round) {
        List<Integer> list = new ArrayList<>();
        List<String> pointParamList = StringUtils.stringToStringList(LIGHTMARKET_RASCAL_POINT, "#");
        for (String pointParam : pointParamList) {
            String[] split = pointParam.split("\\|");
            if (round >= Integer.parseInt(split[0])) {
                list = StringUtils.stringToIntegerList(split[1], ";");
            }
        }
        return list;
    }

    public int randomGetEventIdByEventType(int type) {
        // 总权重
        int sumWeight = 0;
        for (LightMarketEventConfig eventConfig : eventConfigMap.values()) {
            if (eventConfig.getType() != type) {
                continue;
            }
            sumWeight += eventConfig.getWeight();
        }
        // 随机一个权重
        int randomWeight = (int) (Math.random() * sumWeight);
        // 当前权重
        int curWeight = 0;
        for (LightMarketEventConfig eventConfig : eventConfigMap.values()) {
            if (eventConfig.getType() != type) {
                continue;
            }
            curWeight += eventConfig.getWeight();
            if (randomWeight < curWeight) {
                return eventConfig.getId();
            }
        }
        return -1;
    }

    /**
     * 根据类型和索引获取建筑id
     */
    public int getBuildIdByTypeAndIdx(int type, int idx) {
        List<Integer> list = new ArrayList<>();
        for (LightMarketShopConfig shopConfig : getShopConfigMap().values()) {
            if (shopConfig.getType() == type) {
                list.add(shopConfig.getId());
            }
        }
        // 排个序
        Collections.sort(list);
        return list.size() <= idx ? 0 : list.get(idx);
    }

    public Map<Integer, Integer> getJieShiMap() {
        return jieShiMap;
    }

    public List<Integer> getJieShiNpcPos() {
        return jieShiNpcPos;
    }

    public List<Integer> getJieShiMapPath() {
        return jieShiMapPath;
    }

    public int getMapWidth() {
        return mapWidth;
    }

    public void setJieShiMap(Map<Integer, Integer> jieShiMap) {
        this.jieShiMap = jieShiMap;
    }

    public void setJieShiNpcPos(List<Integer> jieShiNpcPos) {
        this.jieShiNpcPos = jieShiNpcPos;
    }

    public void setJieShiMapPath(List<Integer> jieShiMapPath) {
        this.jieShiMapPath = jieShiMapPath;
    }

    public void setMapWidth(int mapWidth) {
        this.mapWidth = mapWidth;
    }
}
